home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / envtools / sphereenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.7 KB  |  121 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    sphereenv -
  19.  *        Make a spherical environment map from a cubical env map
  20.  *    by rendering a reflective sphere.
  21.  *
  22.  *                Paul Haeberli - 1987
  23.  */
  24. #include "texture.h"
  25.  
  26. TEXTURE *tm;
  27. int doreflect;
  28. int size, samples;
  29.  
  30. main(argc,argv)
  31. int argc;
  32. char **argv;
  33. {
  34.     float vr, vg, vb;
  35.     float tr, tg, tb;
  36.     int x, y;
  37.     IMAGE *image;
  38.     int i;
  39.     vect pos, c;
  40.     short *rbuf, *gbuf, *bbuf;
  41.  
  42.     if(argc<5) {
  43.     fprintf(stderr,"usage: sphereenv size samples file.env file.rgb [-s]\n"); 
  44.     exit(1);
  45.     }
  46.     if(argc>5) {
  47.     if(strcmp(argv[5],"-s") == 0)
  48.         doreflect = 1;
  49.     }
  50.     size = atoi(argv[1]);
  51.     samples = atoi(argv[2]);
  52.     tm = tmopen(argv[3]);
  53.     if(!tm) {
  54.     printf("can't open environment map.\n");
  55.     exit(1);
  56.     }
  57.     rbuf = (short *)malloc(size*sizeof(short));
  58.     gbuf = (short *)malloc(size*sizeof(short));
  59.     bbuf = (short *)malloc(size*sizeof(short));
  60.     image = iopen(argv[4],"w",RLE(1),3,size,size,3);
  61.     for(y=0; y<size; y++) {
  62.     for(x=0; x<size; x++) {
  63.         tr = tg = tb = 0.0;
  64.         for(i=0; i<samples; i++) {
  65.         if(samples>1) {
  66.             pos.x = (x+frand())/size-0.5;
  67.             pos.y = (y+frand())/size-0.5;
  68.             pos.z = 0;
  69.         } else {
  70.             pos.x = (float)x/size-0.5;
  71.             pos.y = (float)y/size-0.5;
  72.             pos.z = 0.0;
  73.         }
  74.         shadesphere(&pos,&c);
  75.         tr += c.x;
  76.         tg += c.y;
  77.         tb += c.z;
  78.         }
  79.         rbuf[x] = 255*tr/samples;
  80.         gbuf[x] = 255*tg/samples;
  81.         bbuf[x] = 255*tb/samples;
  82.     }
  83.     putrow(image,rbuf,y,0);
  84.     putrow(image,gbuf,y,1);
  85.     putrow(image,bbuf,y,2);
  86.     tpercentdone(100.0*y/(size-1));
  87.     }
  88.     iclose(image);
  89. }
  90.  
  91. shadesphere(p,c)
  92. vect *p, *c; 
  93. {
  94.     vect e, v, r;
  95.  
  96.     vscale(p,2.02);
  97.     if(spheretovect(p,&v)) {
  98.     if(doreflect) {
  99.         e.x = 0.0;
  100.         e.y = 0.0;
  101.         e.z = 1.0;
  102.         vreflect(&e,&v,&r);
  103.         v = r;
  104.     } 
  105.     lookx(&v);
  106.     envsample(tm,&v,c);
  107.     } else 
  108.     vzero(c);
  109. }
  110.  
  111. lookx(v)
  112. vect *v;
  113. {
  114.     float temp;
  115.  
  116.     temp = v->z;    /* to make us look down the x axis */
  117.     v->z = v->y;
  118.     v->y = -v->x;
  119.     v->x = temp;
  120. }
  121.